package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.AttrKey;
import com.atguigu.lease.model.entity.AttrValue;
import com.atguigu.lease.web.admin.mapper.AttrKeyMapper;
import com.atguigu.lease.web.admin.service.AttrKeyService;
import com.atguigu.lease.web.admin.service.AttrValueService;
import com.atguigu.lease.web.admin.vo.attr.AttrKeyVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author liubo
* @description 针对表【attr_key(房间基本属性表)】的数据库操作Service实现
* @createDate 2023-07-24 15:48:00
*/
@Service
@Slf4j
public class AttrKeyServiceImpl extends ServiceImpl<AttrKeyMapper, AttrKey>
    implements AttrKeyService{

    @Autowired
    private AttrValueService attrValueService;

    @Override
    public List<AttrKeyVo> getAttrInfoList() {
        List<AttrKey> attrKeys = this.list();
        List<Long> AttrKeyIds = attrKeys.stream().map(AttrKey::getId).collect(Collectors.toList());

        LambdaQueryWrapper<AttrValue> attrValueWrapper = Wrappers.lambdaQuery(AttrValue.class);
        attrValueWrapper.in(AttrValue::getAttrKeyId,AttrKeyIds);
        List<AttrValue> attrValues = attrValueService.list(attrValueWrapper);
        Map<Long, List<AttrValue>> attrValueMap = attrValues.stream().collect(Collectors.groupingBy(AttrValue::getAttrKeyId));

        return attrKeys.stream().map(item -> {
            AttrKeyVo attrKeyVo = new AttrKeyVo();
            BeanUtils.copyProperties(item,attrKeyVo);
            attrKeyVo.setAttrValueList(attrValueMap.get(item.getId()));
            return attrKeyVo;
        }).collect(Collectors.toList());
    }
}




